<template>
  <div class="user-name">
    <van-nav-bar
      class="nav-con"
      title="修改昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('closePop')"
      @click-right="onFinish"
    />
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="7"
      placeholder="请输入昵称"
      show-word-limit
    />
  </div>
</template>

<script>
import { editUserProfile } from '@/api/user'
export default {
  name: 'UserName',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      message: this.value
    }
  },
  methods: {
    async onFinish () {
      try {
        if (!this.message.length) {
          this.$toast('昵称不能为空！')
          return
        }
        await editUserProfile({
          name: this.message
        })
        // 更新视图 关闭弹层 提示成功
        this.$emit('input', this.message)
        this.$emit('closePop')
        this.$toast.success('修改成功')
      } catch (err) {
        this.$toast('修改失败，稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
  /deep/.nav-con {
    .van-nav-bar__content {
      background-color: #fff;
      .van-nav-bar__title {
        color: #333;
      }
    }
  }
</style>
